package com.ww.exercise.coroutine.easy.q1

import exercise.coroutine.easy.q1.CommodityService
import exercise.coroutine.easy.q1.OrderService
import exercise.coroutine.easy.q1.Result
import exercise.coroutine.easy.q1.UserService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlin.system.measureTimeMillis

suspend fun getResult(): Result = coroutineScope {
    val userDeferred = async(Dispatchers.IO) {
        UserService.getUserById(1L)
    }
    val orderDeferred = async(Dispatchers.IO) {
        OrderService.getOrderById(1L)
    }
    val commodityDeferred = async(Dispatchers.IO) {
        CommodityService.getCommodityById(1L)
    }
    Result(userDeferred.await(), orderDeferred.await(), commodityDeferred.await())
}

suspend fun main() {
    val totalTime = measureTimeMillis {
        val result = getResult()
        println("函数运行结果$result")
    }
    println("执行结束总耗时：$totalTime")
}